package boun.center;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;

import boun.center.BusSchedule.DayType;
import boun.center.BusSchedule.Direction;

public class Server {
    private final int PORT = 2332;
    private Updater updater;
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Server s = new Server();
        s.runServer();
    }
    
    public Server() {
        updater = new Updater();
        Thread t = new Thread(new Runnable() {  
            public void run() {
                while (true) {
                    try {
                        updater.updateAll();
                        Thread.sleep(200 * 1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }
    
    private void runServer() throws IOException, ClassNotFoundException {
        
        while (true) {
            ServerSocket ss = new ServerSocket(PORT);
            System.out.println("Beginning to accept..");
            Socket s = ss.accept();
            System.out.println("Accepted..");
            System.out.println(s.getRemoteSocketAddress().toString());

            ServiceThread sThread = new ServiceThread(s);
            sThread.start();
            ss.close();
        }
    }
    
    class ServiceThread extends Thread {
    	Socket clientSocket;
    	public ServiceThread(Socket s){
    	    System.out.println("Thread created: " + this.toString());
    		clientSocket = s;
    	}
    	public void run(){
    	    System.out.println("Thread began: " + this.toString());
    		InputStream is;
    		DataPacket dataPacket = null;
			String requestHeader = null;
			ObjectOutputStream oos = null;
    		ObjectInputStream ois = null;
			try {
				is = clientSocket.getInputStream();
				OutputStream os = clientSocket.getOutputStream();
	            ois = new ObjectInputStream(is);
	            oos = new ObjectOutputStream(os);
	            dataPacket = (DataPacket) ois.readObject();
	            requestHeader = dataPacket.getHeader();
	            System.out.println(requestHeader);
	            if (requestHeader.equals(CmpeNews.class.getName())) {
	                ArrayList<CmpeNews> result = updater.getCmpeNews();
					oos.writeObject(result);	
	            } else if (requestHeader.equals(BounNews.class.getName())) {
	                ArrayList<BounNews> result = updater.getBounNews();
	                oos.writeObject(result);
	            } else if (requestHeader.equals(CalendarEvent.class.getName())) {
                    ArrayList<CalendarEvent> result = updater.getCalendar();
                    oos.writeObject(result);
	            } else if (requestHeader.equals(Tweet.class.getName())) {
                    ArrayList<Tweet> result = updater.getTweets();
                    oos.writeObject(result);
	            } else if (requestHeader.equals(BusSchedule.class.getName())) {
	                HashMap<String, Object> map = (HashMap<String, Object>) dataPacket.getData();
	                String routeName = (String) map.get("route_id");
	                BusSchedule.DayType dayType = (DayType) map.get("day_type");
	                BusSchedule.Direction direction = (Direction) map.get("route_dir");
	                ArrayList<String> result = updater.getBusSchedule(routeName, dayType, direction);
	                oos.writeObject(result);
	            }
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					oos.close();
					ois.close();
					clientSocket.close();
					System.out.println("Thread completed: " + this.toString());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} 
    	}
    }
}
